﻿$(document).ready(function () {

    var loading = '<img src="/Content/ajax-loader.gif" alt="Hled upplysingum" />';
    $(".loading").hide();

    //til ad sja medlimi akvedins hops
    $(".see-members").click(function () {
        var groupId = this.id;
        var appendHere = this;
        $(".member-list").empty()
        $(".loading").show();
        //console.log($(".see-members").next("li").text());

        $.ajax({
            type: "GET",
            corntentType: "application/json; charset=utf-8",
            url: "/Group/GetGroupMembers",
            data: { "groupId": groupId },
            dataType: "json",
            success: function (data) {
                $(".loading").hide();
                for (var i in data) {
                    //console.log(data[i].Group.UserOwnerId);

                    if (data[i].UserId == data[i].Group.UserOwnerId) {
                        var admin = data[i].UserProfile.UserName;
                        $(appendHere).next().append('<li><strong><a href=/Account/UserProfile?userName=' + admin + ' >' + admin + ' </a></strong> Stofnandi </li>');

                    } else {

                        var member = data[i].UserProfile.UserName
                        $(appendHere).next().append('<li><a href=/Account/UserProfile?userName=' + member + ' >' + member + ' </a> </li>');

                    }
                }

            }
        })

    });//see-members clickfunction endar


    //baeta vid i hop....
    $("#search-user").click(function (event) {
        //console.log($("#GroupId").attr('value'));
        event.preventDefault();
        $(".find-result").html(loading);
        var search = $("#find-user").val();
        $.ajax({
            type: "GET",
            corntentType: "application/json; charset=utf-8",
            url: "/Account/SearchUser",
            data: { "userName": search },
            dataType: "json",
            success: function (data) {
                $(".find-result").html("");
                if (data.UserName != search) {
                    $(".find-result").append("<strong>Notandi finnst ekki</strong>");
                    return;
                }
                $("#find-user").val("");

                var resultId = data.UserId;
                var groupId = $("#GroupId").attr('value');
                resultId = parseInt(resultId);
                groupId = parseInt(groupId);
                var user = data.UserName;
                //console.log("notandi " + data.UserName);
                var sendData = {
                    "userId": resultId,
                    "groupId": groupId
                };
                $.post("/Group/AddMemberToGroup", sendData, function (res) {
                    //console.log(res);
                    if (res == "ok") {
                        $(".find-result").append("<li><strong>" + user + "</strong> hefur verið bætt í hópinn</li>");
                        SeeMembers();
                    } else {
                        $(".find-result").append("<li><strong>Villa kom upp vinsamlegast reyndu aftur</strong></li>");
                    }
                });


            }
        })
    });//baeta i hop endar

    $("#search-bar").click(function (event) {
        //console.log($("#GroupId").attr('value'));
        event.preventDefault();
        $(".search-result").html(loading);
        var search = $("#searh-result").val();
        $.ajax({
            type: "GET",
            corntentType: "application/json; charset=utf-8",
            url: "/Search/Search",
            data: { "userName": search },
            dataType: "json",
            success: function (data) {
                $(".search-result").html("");
                if (data.UserName != search) {
                    $(".search-result").append("<strong>Notandi finnst ekki</strong>");
                    return;
                }
                $("#search-text").val("");

                var resultId = data.UserId;
                var groupId = $("#GroupId").attr('value');
                resultId = parseInt(resultId);
                groupId = parseInt(groupId);
                var user = data.UserName;
                //console.log("notandi " + data.UserName);
                var sendData = {
                    "userId": resultId,
                    "groupId": groupId
                };


            }
        })
    });




    function SeeMembers() {
        var groupId = $("#GroupId").attr('value');
        $(".member-list").empty();
        $(".member-list").html(loading);
        $.ajax({
            type: "GET",
            corntentType: "application/json; charset=utf-8",
            url: "/Group/GetGroupMembers",
            data: { "groupId": groupId },
            dataType: "json",
            success: function (data) {
                $(".member-list").html("");
                for (var i in data) {
                    //console.log(data[i].Group.UserOwnerId);

                    if (data[i].UserId == data[i].Group.UserOwnerId) {
                        var admin = data[i].UserProfile.UserName;
                        $(".member-list").append('<li><strong><a href=/Account/UserProfile?userName=' + admin + ' >' + admin + ' </a></strong> Stofnandi </li>');

                    } else {
                        var member = data[i].UserProfile.UserName
                        $(".member-list").append('<li><a href=/Account/UserProfile?userName=' + member + ' >' + member + ' </a></li>');

                    }
                }
            }
        });
    }
    SeeMembers();

});